<template>
  <div class="event-wrap">
    <div class="crumbs">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item>
          <i class="el-icon-fa fa-book"></i> 项目管理
        </el-breadcrumb-item>
      </el-breadcrumb>
    </div>

    <div class="container">
      <div class="query-form">
        <el-row :gutter="20">
          <el-col :span="2">
            <el-button @click="create" icon="el-icon-plus">创建</el-button>
          </el-col>
          <el-col :offset="10" :span="3">
            <el-input
                    @keyup.enter.native="query"
                    placeholder="项目名"
                    v-model="queryForm.name"
            />
          </el-col>
          <el-col :span="3">
            <el-input
                    @keyup.enter.native="query"
                    placeholder="团长名"
                    v-model="queryForm.leaderName"
            />
          </el-col>
          <el-col :span="3">
            <el-input
                    @keyup.enter.native="query"
                    placeholder="地区名"
                    v-model="queryForm.areaName"
            />
          </el-col>
          <el-col :span="3">
            <el-button @click="query" icon="el-icon-search" type="primary"
            >搜索
            </el-button>
          </el-col>
        </el-row>
      </div>

      <el-row justify="center" type="flex">
        <el-pagination
                :current-page.sync="pageIndex"
                :page-size="pageSize"
                :total="pageSize * pageCount"
                @current-change="getPage"
                background
                layout="prev, pager, next"
        >
        </el-pagination>
      </el-row>

      <div class="table">
        <el-table :data="tableData" stripe>
          <el-table-column label="项目Id" prop="id" />
          <el-table-column label="项目名" prop="name" width="200px" />
          <el-table-column label="团长" prop="leaderName" />
          <el-table-column label="所属地区" prop="areaName" />
          <el-table-column label="团最大人数" prop="number" />
          <el-table-column label="推荐指数" prop="rate" />
          <el-table-column
                  align="center"
                  label="活动开始时间"
                  prop="time"
                  width="130px"
          />
          <el-table-column label="已选人数" prop="selectedCount" />
          <el-table-column label="最大容量" prop="maxSize" />
          <!--<el-table-column align="center" label="操作" width="200px">-->
            <!--<template slot-scope="scope">-->
              <!--<el-button @click="edit(scope.row.id)" size="mini" type="success"-->
              <!--&gt;编辑-->
              <!--</el-button>-->
              <!--<el-button-->
                      <!--@click="deleteItem(scope.row.id)"-->
                      <!--size="mini"-->
                      <!--type="danger"-->
              <!--&gt;删除-->
              <!--</el-button>-->
            <!--</template>-->
          <!--</el-table-column>-->
        </el-table>
      </div>

      <el-dialog :visible.sync="editing" title="编辑" width="30%">
        <el-form :model="entityForm" label-width="70px" ref="form">
          <el-form-item label="项目名">
            <el-input v-model="entityForm.name"></el-input>
          </el-form-item>
          <el-form-item label="团长">
            <el-select placeholder="请选择团长" v-model="entityForm.leaderId">
              <el-option
                      :key="index"
                      :label="item.name"
                      :value="item.id"
                      v-for="(item, index) in leaders"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="团最大人数">
            <el-input type="number" v-model="entityForm.number"></el-input>
          </el-form-item>
          <el-form-item label="项目开始时间">
            <el-select v-model="eventDay">
              <el-option
                      :key="index"
                      :label="item"
                      :value="index"
                      v-for="(item, index) in days"
              >
              </el-option>
            </el-select>
            <el-select v-model="eventTime">
              <el-option
                      :key="index"
                      :label="item"
                      :value="index"
                      v-for="(item, index) in times"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="活动开始时间">
            <el-input type="number" v-model="startDate"></el-input>
          </el-form-item>
          <el-form-item label="出发地">
            <el-input v-model="entityForm.location"></el-input>
          </el-form-item>
          <el-form-item label="推荐指数">
            <el-input type="number" v-model="entityForm.rate"></el-input>
          </el-form-item>
          <el-form-item label="最大容量">
            <el-input type="number" v-model="entityForm.maxSize"></el-input>
          </el-form-item>

          <el-form-item label="目的地">
            <el-input v-model="entityForm.endLocation"></el-input>
          </el-form-item>
        </el-form>
        <span class="dialog-footer" slot="footer">
          <el-button @click="save" type="primary">确 定</el-button>
          <el-button @click="editing = false">取 消</el-button>
        </span>
      </el-dialog>
    </div>
  </div>
</template>

<script>
  import * as api from "../../api/leader/event";
  import * as leaderApi from "../../api/leader/leader";

  export default {
    name: "LeaderEvent",
    data() {
      return {
        queryForm: {
          areaName: "",
          leaderName: "",
          name: ""
        },
        entityForm: {},
        tableData: [],
        pageSize: api.pageSize,
        pageCount: 1,
        pageIndex: 1,
        editing: false,
        leaders: [],
        eventDay: "",
        eventTime: "",
        startDate: 0,
        days: [
          "星期一",
          "星期二",
          "星期三",
          "星期四",
          "星期五",
          "星期六",
          "星期日"
        ],
        times: [
          "第一场",
          "第二场",
          "第三场",
          "第四场",
          "第五场",
          "第六场",
          "第七场",
          "第八场",
          "第九场"
        ]
      };
    },
    methods: {
      query() {
        api
                .getPageCount(
                        this.queryForm.areaName,
                        this.queryForm.leaderName,
                        this.queryForm.name
                )
                .then(res => {
                  this.pageCount = res;
                  this.pageIndex = 1;
                  this.getPage(1);
                });
      },
      getPage(pageIndex) {
        api
                .getPage(
                        pageIndex,
                        this.queryForm.areaName,
                        this.queryForm.leaderName,
                        this.queryForm.name
                )
                .then(res => {
                  this.tableData = res;
                });
      },
      create() {
        this.entityForm = {
          id: -1,
          leaderId: null,
          name: "",
          number: 2019,
          time: "",
          location: "",
          rate: 2,
          maxSize: 50,
          startDate: null,
          endLocation: null
        };
        this.eventDay = 1;
        this.eventTime = 1;
        this.startDate = 2;
        this.editing = true;
      },
      edit(id) {
        api.get(id).then(res => {
          let split = res.time.split("-");
          this.eventDay = parseInt(split[0]) - 1;
          this.eventTime = parseInt(split[1]) - 1;
          this.startDate = parseInt(split[2]);
          this.entityForm = res;
          this.editing = true;
        });
      },
      save() {
        let day = this.eventDay + 1;
        let time = this.eventTime + 1;
        this.entityForm.time = day + "-" + time + "-" + this.startDate;

        if (this.entityForm.id === -1) {
          api.create(this.entityForm).then(() => {
            this.finishSave();
          });
        } else {
          api.update(this.entityForm).then(() => {
            this.finishSave();
          });
        }
      },
      finishSave() {
        this.$message.success("成功");
        this.getPage(this.pageIndex);
        this.editing = false;
      },
      deleteItem(id) {
        api.deleteItem(id).then(() => {
          this.$message.success("删除成功");
          this.getPage(this.pageIndex);
        });
      },
      getLeaders() {
        leaderApi.listName().then(res => {
          this.leaders = res;
        });
      }
    },
    created() {
      this.query();
      this.getLeaders();
    }
  };
</script>

<style scoped></style>
